const confirmColor = '#1C83E4';

export const Model = {
	_show(option) {
		let _option = Object.assign({}, {
				title: '提示',
				content: '',
				confirmText: '确定',
				cancelText: '取消',
				showCancel: true,

				cancelColor: '#000000',
				confirmColor: confirmColor
			},
			option
		);
		if (typeof _option.content !== 'string') {

			if (_option.content instanceof Error) {

				_option.content = _option.content.toString();

			} else {

				try {
					_option.content = JSON.stringify(_option.content);
				} catch (error) {
					try {
						_option.content = _option.content.toString();
					} catch (error) {
						_option.content = '该错误提示无法正常显示'
					}
				}
			}
		}
		Load.hide();
		uni.showModal(_option);
	},
	/**
	 * 选择
	 * @param {String} content 内容
	 * @param {String} [confirmText=确定] 确定按钮
	 * @param {Object} option 扩展参数
	 */
	select(content = '', confirmText = '确定', option = {}) {
		return new Promise((resolve, reject) => {
			this._show({
				content,
				confirmText,
				...option,
				success: (data) => {
					if (data.confirm) {
						return resolve(content);
					}
					return reject(content);
				},
				fail: () => {
					return reject(content);
				}
			});
		});
	},
	/**
	 * 提示,永远返回true
	 */
	alert(content = '', option = {}) {
		return new Promise((resolve, reject) => {
			this._show({
				content,
				showCancel: false,
				...option,
				success: () => {
					return resolve(content);
				},
				fail: () => {
					return resolve(content);
				}
			});
		});
	},
	/**
	 * 失败 永远返回失败
	 */
	fail(content = '', option = {}) {
		return new Promise((resolve, reject) => {
			this._show({
				content,
				showCancel: false,
				...option,
				success: () => {
					return reject(content);
				},
				fail: () => {
					return reject(content);
				}
			});
		});
	}
};

export const Load = {
	show(title = '加载中...', mask = true) {
		uni.showLoading({
			title,
			mask
		});
	},
	hide() {
		uni.hideLoading();
	}
};

export const Toast = {
	success(title = '成功', duration = 900) {
		uni.showToast({
			title,
			icon: 'success',
			duration
		});
	},
	alert(title = '失败', duration = 2000) {
		uni.showToast({
			title,
			icon: 'none',
			duration
		});
	},
	fail(title = '失败', duration = 2000) {
		uni.showToast({
			title,
			icon: 'none',
			duration
		});
	}
};


/**
 * list
 * @param {Array} list 
 */
export const showActionSheet = (list) => {
	const MAX_PAGE_SIZE = 6;
	let index = 0;

	function getPageList(index, list) {
		// 当前页 以及 之后显示的页面
		let afterlist = (() => {
			let arr = [].concat(list);
			if (index === 0) {
				return arr
			}
			return arr.slice(5 + 4 * (index - 1));
		})();
		let l = afterlist.length;

		// 第一页的情况
		if (index === 0) {
			if (l > MAX_PAGE_SIZE) {
				let arr = afterlist.slice(0, 5);
				arr.push('下一页')
				return arr;
			}
			return afterlist;
		}

		// 大于5条, 就显示4条
		if (l > (MAX_PAGE_SIZE - 1)) {
			let arr = afterlist.slice(0, 4);
			arr.push('下一页')
			arr.unshift('上一页')
			return arr;
		}

		// 小于5条全部显示
		let arr = afterlist.slice(0, 5);
		arr.unshift('上一页')
		return arr;
	}

	return new Promise((resolve, reject) => {
		let done = (i) => {
			let arr = getPageList(i, list);
			uni.showActionSheet({
				itemList: arr,
				success: (res) => {
					if (arr[res.tapIndex] === '上一页') {
						return done(--index);
					}
					if (arr[res.tapIndex] === '下一页') {
						return done(++index);
					}
					resolve({
						index: res.tapIndex,
						value: arr[res.tapIndex]
					});
				},
				fail: (err) => {
					reject(err);
				}
			})
		}
		done(index);
	});
}


/**
 * request
 * @param {Object} api 请求对象
 * @param {Object} options {loadText, showModal, modelText}
 */
export const Request = async function(api, options = {}) {
	options = Object.assign({
		loadText: '加载中...',
		showModal: true,
		modelText: ''
	}, options)
	let result = null;
	Load.show(options.loadText);
	try {
		result = await api;
	} catch (error) {
		let errMsg = null;
		Load.hide();
		if (options.showModal) {
			if (options.modelText) {
				if (typeof options.modelText === 'function') {
					errMsg = options.modelText(error);
				} else {
					errMsg = options.modelText;
				}
			} else {
				errMsg = JSON.stringify(error)
			}
			await Model.alert(errMsg);
		}
		return Promise.reject(error);
	}
	Load.hide();
	return result;
}

export default {
	Model,
	Load,
	Toast,
	Request,
	showActionSheet
};